﻿// ***********************************************************************
// Assembly         : INNOVACALL.Framework.Common
// Author           : Lolo
// Created          : 09-23-2013
//
// Last Modified By : Lolo
// Last Modified On : 09-23-2013
// ***********************************************************************
// <copyright file="LocalizationAttribute.cs" company="">
//     Copyright (c) . All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Threading;
using System.Globalization;

namespace INNOVACALL.Framework.Common.MVCHelpers.Localization
{
    /// <summary>
    /// Class LocalizationAttribute.
    /// Inspired from http://geekswithblogs.net/shaunxu/archive/2012/09/04/localization-in-asp.net-mvc-ndash-upgraded.aspx
    /// </summary>
    public class LocalizationAttribute : ActionFilterAttribute
    {
        public const string cstRouteDataLangName = "language";
        public const string cstCultureCookieName = "InnovacallMVCPortalCurrentUserCulture";

        /// <summary>
        /// Appelée par l'infrastructure ASP.NET MVC avant l'exécution de la méthode d'action.
        /// </summary>
        /// <param name="filterContext">Contexte du filtre.</param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.RouteData.Values[cstRouteDataLangName] != null &&
                !string.IsNullOrWhiteSpace(filterContext.RouteData.Values[cstRouteDataLangName].ToString()))
            {
                // set the culture from the route data (url)
                var lang = filterContext.RouteData.Values[cstRouteDataLangName].ToString();
                Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(lang);
            }
            else
            {
                // load the culture info from the cookie
                var cookie = filterContext.HttpContext.Request.Cookies[cstCultureCookieName];
                var langHeader = string.Empty;
                if (cookie != null)
                {
                    // set the culture by the cookie content
                    langHeader = cookie.Value;
                    Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(langHeader);
                }
                else
                {
                    // set the culture by the location if not speicified
                    langHeader = filterContext.HttpContext.Request.UserLanguages[0];
                    Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(langHeader);
                }
                // set the lang value into route data
                filterContext.RouteData.Values[cstRouteDataLangName] = langHeader;
            }

            // save the location into cookie
            HttpCookie _cookie = new HttpCookie(cstCultureCookieName, Thread.CurrentThread.CurrentUICulture.Name);
            _cookie.Expires = DateTime.Now.AddYears(1);
            filterContext.HttpContext.Response.SetCookie(_cookie);

            base.OnActionExecuting(filterContext);
        }
    }
}
